//获取应用实例
const app = getApp()
import confg from '../../utils/config';
import util from '../../utils/util';
import {
  Banner,
  HomeList
} from '../../utils/api'
Page({
  data: {
    confg:confg,
    task_type_index:0,
    disp:'none',
    banners: [],
    list: [],
    curpage: 1,
    size: 10,
    isLoadMore: true,

    
  },

  //事件处理函数
  bindViewTap: function () {

  },
  onLoad: function () {
    Banner().then(res => {
      this.setData({
        banners: res.data
      })
    })
    this.getList();
  },
  onShow: function () { 
 
  },
   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (ops) {
    let shares = confg.share_text
    //  {
    //   title: '引客来 网红达人 带货 种草',
    //   imageUrl: '/images/home-share.png',
    //   path: `/pages/index/index`
    // }
    return shares
  },
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this.setData({
        curpage: this.data.curpage + 1,
      })
      this.getList()
    }
  },
  // 获取首页数据
  getList(boo) {
    let da = {
      limit: this.data.size,
      platform:this.data.task_type_index,
      page: this.data.curpage - 1
    }
    HomeList(da).then(res => {
      let goods = [];
      if (res.data.length < this.data.size) {
        this.setData({
          isLoadMore: false
        })
      }
      goods = this.data.list.concat(res.data)
      this.setData({
        list: goods
      })
      if(boo){wx.stopPullDownRefresh()}
    })
  },
  // 去详情
  goViews(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id
    })
  },
  // 查看banner
  viewBanner(e) {
    let url = confg.base_url + e.currentTarget.dataset.url
    wx.previewImage({
      current: url,
      urls: [url]
    });
  },
  hideshowFn(e){
    util.checkLogin(res=>{
      this.setData({
        disp:e.currentTarget.dataset.disp
      })
    },1)
  },
  cutTaskTypeFn(e){
    this.setData({
      task_type_index: e.currentTarget.dataset.id,
      curpage: 1,
      list: []
    })
    this.getList();
  },
  addTask(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/addtask/addtask?lv=1&id=' + id
    })
  },
  onPullDownRefresh() {
    this.setData({
      curpage: 1,
      list: []
    })
    this.getList(true);
  }

})